<!DOCTYPE html>
<html lang="en">
  <head>
    <title>PerfectLib  Reference</title>
    <link rel="stylesheet" type="text/css" href="css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="css/highlight.css" />
    <meta charset="utf-8">
    <script src="js/jquery.min.js" defer></script>
    <script src="js/jazzy.js" defer></script>
    
  </head>
  <body>


    <a title="PerfectLib  Reference"></a>

    <header class="header">
      <p class="header-col header-col--primary">
        <a class="header-link" href="index.html">
          PerfectLib Docs
        </a>
         (70% documented)
      </p>
    
        <p class="header-col header-col--secondary">
          <a class="header-link" href="https://github.com/PerfectlySoft/Perfect">
            <img class="header-icon" src="img/gh.png"/>
            View on GitHub
          </a>
        </p>
    
    </header>

    <p class="breadcrumbs">
      <a class="breadcrumb" href="index.html">PerfectLib Reference</a>
      <img class="carat" src="img/carat.png" />
      PerfectLib  Reference
    </p>

    <div class="content-wrapper">
      <nav class="navigation">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="Classes.html">Classes</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/Bytes.html">Bytes</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/File.html">File</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/File/OpenMode.html">– OpenMode</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/HTTP2Client.html">HTTP2Client</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/HTTPServer.html">HTTPServer</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/JSONConvertibleObject.html">JSONConvertibleObject</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/JSONDecoding.html">JSONDecoding</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/MimeReader.html">MimeReader</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/MimeReader/BodySpec.html">– BodySpec</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/MustacheEvaluationContext.html">MustacheEvaluationContext</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/MustacheEvaluationOutputCollector.html">MustacheEvaluationOutputCollector</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/MustacheGroupTag.html">MustacheGroupTag</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/MustacheParser.html">MustacheParser</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/MustachePartialTag.html">MustachePartialTag</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/MustachePragmaTag.html">MustachePragmaTag</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/MustacheTag.html">MustacheTag</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/MustacheTemplate.html">MustacheTemplate</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/SysProcess.html">SysProcess</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Classes/TemporaryFile.html">TemporaryFile</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="Global Variables.html">Global Variables</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Global Variables.html#/s:v10PerfectLib10fileStderrCS_4File">fileStderr</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Global Variables.html#/s:v10PerfectLib9fileStdinCS_4File">fileStdin</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Global Variables.html#/s:v10PerfectLib10fileStdoutCS_4File">fileStdout</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Global Variables.html#/s:v10PerfectLib22serverPerfectLibrariesSS">serverPerfectLibraries</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="Enums.html">Enums</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Enums/HTTPFilterPriority.html">HTTPFilterPriority</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Enums/HTTPMethod.html">HTTPMethod</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Enums/HTTPRequestFilterResult.html">HTTPRequestFilterResult</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Enums/HTTPRequestHeader.html">HTTPRequestHeader</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Enums/HTTPRequestHeader/Name.html">– Name</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Enums/HTTPResponseFilterResult.html">HTTPResponseFilterResult</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Enums/HTTPResponseHeader.html">HTTPResponseHeader</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Enums/HTTPResponseHeader/Name.html">– Name</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Enums/HTTPResponseStatus.html">HTTPResponseStatus</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Enums/JSONConversionError.html">JSONConversionError</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Enums/MustacheError.html">MustacheError</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Enums/PerfectError.html">PerfectError</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="Extensions.html">Extensions</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Extensions/Array.html">Array</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Extensions/Bool.html">Bool</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Extensions/Dictionary.html">Dictionary</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Extensions/Double.html">Double</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Extensions/Int.html">Int</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Extensions/NetNamedPipe.html">NetNamedPipe</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Extensions/Optional.html">Optional</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Extensions/String.html">String</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Extensions/UInt.html">UInt</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Extensions/UnicodeScalar.html">UnicodeScalar</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="Functions.html">Functions</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Functions.html#/s:ZF10PerfectLiboi2eeFTOOS_17HTTPRequestHeader4NameS1__Sb">==(_:_:)</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Functions.html#/s:ZF10PerfectLiboi2eeFTOOS_18HTTPResponseHeader4NameS1__Sb">==(_:_:)</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Functions.html#/s:ZF10PerfectLiboi2eeFTOS_10HTTPMethodS0__Sb">==(_:_:)</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Functions.html#/s:F10PerfectLib10empty_uuidFT_TVs5UInt8S0_S0_S0_S0_S0_S0_S0_S0_S0_S0_S0_S0_S0_S0_S0__">empty_uuid()</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Functions.html#/s:F10PerfectLib10formatDateFzTSd6formatSS8timezoneGSqSS_6localeGSqSS__SS">formatDate(_:format:timezone:locale:)</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Functions.html#/s:F10PerfectLib6getNowFT_Sd">getNow()</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Functions.html#/s:F10PerfectLib16icuDateToSecondsFSdSi">icuDateToSeconds(_:)</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Functions.html#/s:F10PerfectLib15mustacheRequestFT7requestPS_11HTTPRequest_8responsePS_12HTTPResponse_7handlerPS_19MustachePageHandler_12templatePathSS_T_">mustacheRequest(request:response:handler:templatePath:)</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Functions.html#/s:F10PerfectLib11random_uuidFT_TVs5UInt8S0_S0_S0_S0_S0_S0_S0_S0_S0_S0_S0_S0_S0_S0_S0__">random_uuid()</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Functions.html#/s:F10PerfectLib16secondsToICUDateFSiSd">secondsToICUDate(_:)</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="Protocols.html">Protocols</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Protocols/HTTPRequest.html">HTTPRequest</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Protocols/HTTPRequestFilter.html">HTTPRequestFilter</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Protocols/HTTPResponse.html">HTTPResponse</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Protocols/HTTPResponseFilter.html">HTTPResponseFilter</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Protocols/HeaderListener.html">HeaderListener</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Protocols/JSONConvertible.html">JSONConvertible</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Protocols/MustachePageHandler.html">MustachePageHandler</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="Structs.html">Structs</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Structs/Dir.html">Dir</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Structs/Encoding.html">Encoding</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Structs/GenerateFromPointer.html">GenerateFromPointer</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Structs/HTTPCookie.html">HTTPCookie</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Structs/HTTPCookie/Expiration.html">– Expiration</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Structs/JSONConvertibleNull.html">JSONConvertibleNull</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Structs.html#/s:V10PerfectLib3Log">Log</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Structs/MimeType.html">MimeType</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Structs/MimeType/TopType.html">– TopType</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Structs/PerfectServer.html">PerfectServer</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Structs/RouteMap.html">RouteMap</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Structs/Routing.html">Routing</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Structs/StaticFileHandler.html">StaticFileHandler</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="Structs/UTF8Encoding.html">UTF8Encoding</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">

        <section class="section">
          <div class="section-content">
            
            <a href='#perfect-server-side-swift' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h1 id='perfect-server-side-swift'>Perfect: Server-Side Swift</h1>

<p><a href="http://perfect.org/get-involved.html"><img src="http://www.perfect.org/images/perfect-git-banner.png" alt="Perfect logo"></a></p>

<p><a href="https://developer.apple.com/swift/"><img src="https://img.shields.io/badge/Swift-3.0-orange.svg?style=flat" alt="Swift 3.0"></a>
<a href="https://developer.apple.com/swift/"><img src="https://img.shields.io/badge/Platforms-OS%20X%20%7C%20Linux%20-lightgray.svg?style=flat" alt="Platforms OS X | Linux"></a>
<a href="http://perfect.org/licensing.html"><img src="https://img.shields.io/badge/License-Apache-lightgrey.svg?style=flat" alt="License Apache"></a>
<a href="http://www.perfect.org/docs/"><img src="https://img.shields.io/badge/docs-99%25-yellow.svg?style=flat" alt="Docs"></a>
<a href="https://github.com/PerfectlySoft/Perfect/issues"><img src="https://img.shields.io/github/issues/PerfectlySoft/Perfect.svg" alt="GitHub issues"></a>
<a href="https://codebeat.co/projects/github-com-perfectlysoft-perfect"><img src="https://codebeat.co/badges/85f8f628-6ce8-4818-867c-21b523484ee9" alt="codebeat"></a>
<a href="http://twitter.com/PerfectlySoft"><img src="https://img.shields.io/badge/Twitter-@PerfectlySoft-brightgreen.svg?style=flat" alt="Twitter"></a>
<a href="https://gitter.im/PerfectlySoft/Perfect?utm_source=badge&amp;utm_medium=badge&amp;utm_campaign=pr-badge&amp;utm_content=badge"><img src="https://img.shields.io/badge/Gitter-Join%20Chat-brightgreen.svg" alt="Join the chat at https://gitter.im/PerfectlySoft/Perfect"></a></p>

<p><strong>The master branch of this project currently compiles with the default Swift 3.0 toolchain included in Xcode 8 beta 2. On Ubuntu use the <em>DEVELOPMENT-SNAPSHOT-2016-06-20-a</em> toolchain, released June 20th.</strong></p>

<p><strong>Important:</strong> On OS X you must set the Xcode command line tools preference as follows:
<img src="http://www.perfect.org/docs/assets/xcode_prefs.png" alt="Xcode Prefs"> </p>

<p>If you do not do this you will experience compile time errors when using SPM on the command line.</p>

<p>If you are still having build problems with any of the code in our repositories, try doing a clean build with Swift Package Manager by typing:</p>

<p><code>swift build --clean=dist ; swift build</code></p>

<p>&ndash;</p>

<p>Perfect is an application server for Linux or OS X which provides a framework for developing web and other REST services in the Swift programming language. Its primary focus is on facilitating mobile apps which require backend server software, enabling you to use one language for both front and back ends.</p>

<p>Perfect operates using either its own stand-alone HTTP/HTTPS server or through FastCGI. It provides a system for loading your own Swift based modules at startup and for interfacing those modules with its request/response objects or to the built-in mustache template processing system.</p>

<p>Perfect is built on its own high performance completely asynchronous networking engine with the goal of providing a scalable option for internet services. It supports SSL out of the box and provides a suite of tools which are commonly required by internet servers, such as WebSockets and iOS push notifications, but does not limit your options. Feel free to swap in your own favorite JSON or templating systems, etc.</p>
<a href='#quick-start' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h2 id='quick-start'>Quick Start</h2>
<a href='#swift-3-0' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='swift-3-0'>Swift 3.0</h3>

<p>Ensure you have properly installed a Swift 3.0 toolchain from <a href="https://swift.org/getting-started/">Swift.org</a>. In the terminal, typing:</p>
<pre class="highlight plaintext"><code>swift --version
</code></pre>

<p>should produce something like the following:</p>
<pre class="highlight plaintext"><code>Apple Swift version 3.0 (swiftlang-800.0.33.1 clang-800.0.31)
Target: x86_64-apple-macosx10.9
</code></pre>
<a href='#os-x' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='os-x'>OS X</h3>

<p>Perfect relies on <a href="http://brew.sh">Home Brew</a> for installing dependencies on OS X. This is currently limited to OpenSSL. To install Home Brew, in the Terminal, type:</p>
<pre class="highlight plaintext"><code>/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
</code></pre>

<p>To install OpenSSL:</p>
<pre class="highlight plaintext"><code>brew install openssl
brew link openssl --force
</code></pre>
<a href='#linux' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='linux'>Linux</h3>

<p>Perfect relies on OpenSSL, libssl-dev and uuid:</p>
<pre class="highlight plaintext"><code>sudo apt-get install openssl libssl-dev uuid-dev
</code></pre>
<a href='#build-starter-project' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='build-starter-project'>Build Starter Project</h3>

<p>The following will clone and build an empty starter project and launch the server on port 8181.</p>
<pre class="highlight plaintext"><code>git clone https://github.com/PerfectlySoft/PerfectTemplate.git
cd PerfectTemplate
swift build
.build/debug/PerfectTemplate
</code></pre>

<p>You should see the following output:</p>
<pre class="highlight plaintext"><code>Starting HTTP server on 0.0.0.0:8181 with document root ./webroot
</code></pre>

<p>This means the server is running and waiting for connections. Access <a href="http://127.0.0.1:8181/">http://localhost:8181/</a> to see the greeting. Hit control-c to terminate the server.</p>

<p>You can view the full source code for <a href="https://github.com/PerfectlySoft/PerfectTemplate">PerfectTemplate</a>. </p>
<a href='#xcode' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='xcode'>Xcode</h3>

<p>Swift Package Manager can generate an Xcode project which can run the PerfectTemplate server and provide full source code editing and debugging for your project. Enter the following in your terminal:</p>
<pre class="highlight plaintext"><code>swift package generate-xcodeproj
</code></pre>

<p>Open the generated file <q>PerfectTemplate.xcodeproj</q>. Ensure that you have selected the executable target and selected it to run on <q>My Mac</q>. You can now run and debug the server.</p>
<a href='#next-steps' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h2 id='next-steps'>Next Steps</h2>

<p>These example snippets show how to accomplish several common tasks that one might need to do when developing a Web/REST application. In all cases, the <code>request</code> and <code>response</code> variables refer, respectively, to the <code>HTTPRequest</code> and <code>HTTPResponse</code> objects which are given to your URL handlers.</p>

<p>Consult the <a href="http://www.perfect.org/docs/">API reference</a> for more details.</p>
<a href='#get-a-client-request-header' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='get-a-client-request-header'>Get a client request header</h3>
<pre class="highlight swift"><code><span class="k">if</span> <span class="k">let</span> <span class="nv">acceptEncoding</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="nf">header</span><span class="p">(</span><span class="o">.</span><span class="n">acceptEncoding</span><span class="p">)</span> <span class="p">{</span>
    <span class="o">...</span>
<span class="p">}</span>
</code></pre>
<a href='#get-client-get-or-post-parameters' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='get-client-get-or-post-parameters'>Get client GET or POST parameters</h3>
<pre class="highlight swift"><code><span class="k">if</span> <span class="k">let</span> <span class="nv">foo</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="nf">param</span><span class="p">(</span><span class="nv">name</span><span class="p">:</span> <span class="s">"foo"</span><span class="p">)</span> <span class="p">{</span>
    <span class="o">...</span>
<span class="p">}</span>   
<span class="k">if</span> <span class="k">let</span> <span class="nv">foo</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="nf">param</span><span class="p">(</span><span class="nv">name</span><span class="p">:</span> <span class="s">"foo"</span><span class="p">,</span> <span class="n">defaultValue</span><span class="p">:</span> <span class="s">"default foo"</span><span class="p">)</span> <span class="p">{</span>
    <span class="o">...</span>
<span class="p">}</span>
<span class="k">let</span> <span class="nv">foos</span><span class="p">:</span> <span class="p">[</span><span class="kt">String</span><span class="p">]</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="nf">params</span><span class="p">(</span><span class="nv">named</span><span class="p">:</span> <span class="s">"foo"</span><span class="p">)</span>
</code></pre>
<a href='#get-the-current-request-path' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='get-the-current-request-path'>Get the current request path</h3>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">path</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">path</span>
</code></pre>
<a href='#access-the-server-39-s-document-directory-and-return-an-image-file-to-the-client' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='access-the-server-39-s-document-directory-and-return-an-image-file-to-the-client'>Access the server&rsquo;s document directory and return an image file to the client</h3>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">docRoot</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">documentRoot</span>
<span class="k">do</span> <span class="p">{</span>
    <span class="k">let</span> <span class="nv">mrPebbles</span> <span class="o">=</span> <span class="kt">File</span><span class="p">(</span><span class="s">"</span><span class="se">\(</span><span class="n">docRoot</span><span class="se">)</span><span class="s">/mr_pebbles.jpg"</span><span class="p">)</span>
    <span class="k">let</span> <span class="nv">imageSize</span> <span class="o">=</span> <span class="n">mrPebbles</span><span class="o">.</span><span class="n">size</span>
    <span class="k">let</span> <span class="nv">imageBytes</span> <span class="o">=</span> <span class="k">try</span> <span class="n">mrPebbles</span><span class="o">.</span><span class="nf">readSomeBytes</span><span class="p">(</span><span class="nv">count</span><span class="p">:</span> <span class="n">imageSize</span><span class="p">)</span>
    <span class="n">response</span><span class="o">.</span><span class="nf">setHeader</span><span class="p">(</span><span class="o">.</span><span class="n">contentType</span><span class="p">,</span> <span class="nv">value</span><span class="p">:</span> <span class="kt">MimeType</span><span class="o">.</span><span class="nf">forExtension</span><span class="p">(</span><span class="s">"jpg"</span><span class="p">))</span>
    <span class="n">response</span><span class="o">.</span><span class="nf">setHeader</span><span class="p">(</span><span class="o">.</span><span class="n">contentLength</span><span class="p">,</span> <span class="nv">value</span><span class="p">:</span> <span class="s">"</span><span class="se">\(</span><span class="n">imageBytes</span><span class="o">.</span><span class="n">count</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
    <span class="n">response</span><span class="o">.</span><span class="nf">appendBody</span><span class="p">(</span><span class="nv">bytes</span><span class="p">:</span> <span class="n">imageBytes</span><span class="p">)</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">{</span>
    <span class="n">response</span><span class="o">.</span><span class="n">status</span> <span class="o">=</span> <span class="o">.</span><span class="n">internalServerError</span>
    <span class="n">response</span><span class="o">.</span><span class="nf">appendBody</span><span class="p">(</span><span class="nv">string</span><span class="p">:</span> <span class="s">"Error handling request: </span><span class="se">\(</span><span class="n">error</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="p">}</span>
<span class="n">response</span><span class="o">.</span><span class="nf">completed</span><span class="p">()</span>
</code></pre>
<a href='#get-client-cookies' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='get-client-cookies'>Get client cookies</h3>
<pre class="highlight swift"><code><span class="k">for</span> <span class="p">(</span><span class="n">cookieName</span><span class="p">,</span> <span class="n">cookieValue</span><span class="p">)</span> <span class="k">in</span> <span class="n">request</span><span class="o">.</span><span class="n">cookies</span> <span class="p">{</span>
    <span class="o">...</span>
<span class="p">}</span>
</code></pre>
<a href='#set-client-cookie' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='set-client-cookie'>Set client cookie</h3>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">cookie</span> <span class="o">=</span> <span class="kt">HTTPCookie</span><span class="p">(</span><span class="nv">name</span><span class="p">:</span> <span class="s">"cookie-name"</span><span class="p">,</span> <span class="nv">value</span><span class="p">:</span> <span class="s">"the value"</span><span class="p">,</span> <span class="nv">domain</span><span class="p">:</span> <span class="kc">nil</span><span class="p">,</span>
                    <span class="nv">expires</span><span class="p">:</span> <span class="o">.</span><span class="n">session</span><span class="p">,</span> <span class="nv">path</span><span class="p">:</span> <span class="s">"/"</span><span class="p">,</span>
                    <span class="nv">secure</span><span class="p">:</span> <span class="kc">false</span><span class="p">,</span> <span class="nv">httpOnly</span><span class="p">:</span> <span class="kc">false</span><span class="p">)</span>
<span class="n">response</span><span class="o">.</span><span class="nf">addCookie</span><span class="p">(</span><span class="n">cookie</span><span class="p">)</span>
</code></pre>
<a href='#return-json-data-to-client' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='return-json-data-to-client'>Return JSON data to client</h3>
<pre class="highlight swift"><code><span class="n">response</span><span class="o">.</span><span class="nf">setHeader</span><span class="p">(</span><span class="o">.</span><span class="n">contentType</span><span class="p">,</span> <span class="nv">value</span><span class="p">:</span> <span class="s">"application/json"</span><span class="p">)</span>
<span class="k">let</span> <span class="nv">d</span><span class="p">:</span> <span class="p">[</span><span class="kt">String</span><span class="p">:</span><span class="kt">Any</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="s">"a"</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span> <span class="s">"b"</span><span class="p">:</span><span class="mf">0.1</span><span class="p">,</span> <span class="s">"c"</span><span class="p">:</span> <span class="kc">true</span><span class="p">,</span> <span class="s">"d"</span><span class="p">:[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">]]</span>

<span class="k">do</span> <span class="p">{</span>
    <span class="k">try</span> <span class="n">response</span><span class="o">.</span><span class="nf">setBody</span><span class="p">(</span><span class="nv">json</span><span class="p">:</span> <span class="n">d</span><span class="p">)</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">{</span>
    <span class="c1">//...</span>
<span class="p">}</span>
<span class="n">response</span><span class="o">.</span><span class="nf">completed</span><span class="p">()</span>
</code></pre>

<p><em>This snippet uses the built-in JSON encoding. Feel free to bring in your own favorite JSON encoder/decoder.</em></p>
<a href='#repository-layout' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h2 id='repository-layout'>Repository Layout</h2>

<p>We have finished refactoring Perfect to support Swift Package Manager. The Perfect project has been split up into the following repositories:</p>

<ul>
<li><a href="https://github.com/PerfectlySoft/Perfect">Perfect</a> - This repository contains the core PerfectLib and will continue to be the main landing point for the project.</li>
<li><a href="https://github.com/PerfectlySoft/PerfectTemplate">PerfectTemplate</a> - A simple starter project which compiles with SPM into a stand-alone executable HTTP server. This repository is ideal for starting on your own Perfect based project.</li>
<li><a href="https://github.com/PerfectlySoft/PerfectDocs">PerfectDocs</a> - Contains all API reference related material.</li>
<li><a href="https://github.com/PerfectlySoft/PerfectExamples">PerfectExamples</a> - All the Perfect example projects and documentation.</li>
<li><a href="https://github.com/PerfectlySoft/PerfectTemplateFCGI">PerfectTemplateFCGI</a> - A simple starter project which compiles with SPM into a FastCGI server suitable for use with Apache 2.4 or NGINX. This repository is ideal for starting on your own Perfect based project if you are required to use an existing server which supports FastCGI.</li>
<li><a href="https://github.com/PerfectlySoft/Perfect-Redis">Perfect-Redis</a> - Redis database connector.</li>
<li><a href="https://github.com/PerfectlySoft/Perfect-SQLite">Perfect-SQLite</a> - SQLite3 database connector.</li>
<li><a href="https://github.com/PerfectlySoft/Perfect-PostgreSQL">Perfect-PostgreSQL</a> - PostgreSQL database connector.</li>
<li><a href="https://github.com/PerfectlySoft/Perfect-MySQL">Perfect-MySQL</a> - MySQL database connector.</li>
<li><a href="https://github.com/PerfectlySoft/Perfect-MongoDB">Perfect-MongoDB</a> - MongoDB database connector.</li>
<li><a href="https://github.com/PerfectlySoft/Perfect-FastCGI-Apache2.4">Perfect-FastCGI-Apache2.4</a> - Apache 2.4 FastCGI module; required for the Perfect FastCGI server variant.</li>
</ul>

<p>The database connectors are all stand-alone and can be used outside of the Perfect framework and server.</p>
<a href='#further-information' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h2 id='further-information'>Further Information</h2>

<p>For more information on the Perfect project, please visit <a href="http://perfect.org">perfect.org</a>.</p>

          </div>
        </section>


      </article>
    </div>
    <section class="footer">
      <p>© 2016 PerfectlySoft Inc. and the Perfect project authors</p>
      <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.7.0</a>, a <a class="link" href="http://realm.io" target="_blank" rel="external">Realm</a> project.</p>
    </section>
  </body>
</div>
</html>
